package net.lainiao.service.search;


import com.alibaba.fastjson.JSONObject;
import net.lainiao.base.search.SearchModel;
import net.lainiao.service.dao.BaseTest;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Before;
import org.junit.Test;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;

/**
 * Created by Administrator on 2017/1/16.
 */
public class ClientTest2 extends BaseTest {

    private Client client=null;
    private IndexRequestBuilder indexRequestBuilder=null;

    @Before
    public void CreateClient() {
        //设置集群名称
        Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
        try {
            InetAddress address = InetAddress.getByName("127.0.0.1");
            client = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new InetSocketTransportAddress(address, 9300));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        indexRequestBuilder = client.prepareIndex("codesearch","doc");
    }


    @Test
    public void testAppendDoc() {
        SearchModel model = new SearchModel();
        model.setLink("http://www.baidu.com");
        model.setContent("百度");
        String json= JSONObject.toJSONString(model);
        System.out.println(json);
        indexRequestBuilder.setSource(json).setId("0").execute().actionGet();
    }

    @Test
    public void testAppendDoc2() {
        SearchModel model = new SearchModel();
        model.setLink("http://www.baidu.com");
        model.setTitle("百度");
        model.setContent("百度是一家大型的公司");
        String json= JSONObject.toJSONString(model);
        System.out.println(json);
        indexRequestBuilder.setSource(json).setId("1").execute().actionGet();
    }


    @Test
    public void TestMulitSearch(){
        QueryBuilder queryBuilder= QueryBuilders.multiMatchQuery("百度 大型","title","content");
        HighlightBuilder highlightBuilder=new HighlightBuilder();
        highlightBuilder.field("title").field("content");
        SearchResponse response=client.prepareSearch("codesearch").setTypes("doc").setQuery(queryBuilder).highlighter(highlightBuilder).execute().actionGet();
        SearchHits searchHits=response.getHits();
        System.out.println("查询到记录数="+ searchHits.getTotalHits());
        SearchHit[] searchHitses=searchHits.getHits();
        for(SearchHit searchHit : searchHits){
            Map map=searchHit.getSource();
            System.out.println("title:"+map.get("title"));
            System.out.println("content:"+map.get("content"));
            Map map1=searchHit.getHighlightFields();
            if(map1.get("content")!=null){
                Text[] htmls=((HighlightField)map1.get("content")).getFragments();
                StringBuilder stringBuilder=new StringBuilder();
                for(Text text:htmls){
                    stringBuilder.append(text.toString());
                }
                System.out.println("Hit Map <content>:"+stringBuilder.toString());
            }

            if(map1.get("title")!=null){
                Text[] htmls=((HighlightField)map1.get("title")).getFragments();
                StringBuilder stringBuilder=new StringBuilder();
                for(Text text:htmls){
                    stringBuilder.append(text.toString());
                }
                System.out.println("Hit Map <title>:"+stringBuilder.toString());
            }
        }
    }

}
